package com.ops.controller;

import com.ops.entity.Report;
import com.ops.entity.OpsRequest;
import com.ops.entity.OpsTask;
import com.ops.service.ReportService;
import com.ops.service.OpsTaskService;
import com.ops.service.FaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 报告控制器
 * 处理报告相关的请求，包括报告生成、查看等功能
 */
@Controller
public class ReportController {
    
    @Autowired
    private ReportService reportService;
    
    @Autowired
    private OpsTaskService opsTaskService;
    
    @Autowired
    private FaultService faultService;
    
    @GetMapping("/index")
    public String index() {
        return "index";
    }
    
    /**
     * 创建报告
     * 处理报告创建请求，需要用户认证
     * 
     * @param report 报告数据
     * @param userDetails 当前登录用户信息
     * @return 创建成功的报告
     */
    @PostMapping("/api/reports")
    @ResponseBody
    public ResponseEntity<Report> createReport(
        @RequestBody Report report,
        @AuthenticationPrincipal UserDetails userDetails
    ) {
        Report savedReport = reportService.saveReport(report, userDetails.getUsername());
        return ResponseEntity.ok(savedReport);
    }
    @GetMapping("/homePage/{uuid}")
    public String homePage(@PathVariable String uuid, Model model) {
        Report report = reportService.getReportByUuid(uuid);

        model.addAttribute("report", report);
        return "homePage";
    }
    /**
     * 查看报告详情
     * 获取报告信息并计算统计数据
     * 
     * @param uuid 报告UUID
     * @param model 视图模型
     * @return 报告详情页面
     */
    @GetMapping("/reports/{uuid}")
    public String viewReport(@PathVariable String uuid, Model model) {
        Report report = reportService.getReportByUuid(uuid);
        
        // 获取运维任务统计
        Map<String, Object> taskStats = opsTaskService.getTaskStats(uuid);
        report.setTotalRequests((Integer) taskStats.get("totalRequests"));
        report.setTotalOpsHours((Double) taskStats.get("totalOpsHours"));
        
        // 获取故障统计
        Map<String, Object> faultStats = faultService.getFaultStats(uuid);
        report.setTotalFaults((Integer) faultStats.get("totalFaults"));
        report.setThirdPartyFaults((Integer) faultStats.get("thirdPartyFaults"));
        report.setThirdPartyResolvedFaults((Integer) faultStats.get("thirdPartyResolvedFaults"));
        report.setInternalFaults((Integer) faultStats.get("internalFaults"));
        report.setInternalResolvedFaults((Integer) faultStats.get("internalResolvedFaults"));
        
        model.addAttribute("report", report);
        return "summary";
    }
    
    @GetMapping("/reports/{uuid}/top-requests")
    public String showTopRequests(
        @PathVariable String uuid,
        @RequestParam(defaultValue = "0") int page,
        Model model
    ) {
        Report report = reportService.getReportByUuid(uuid);
        if (report == null) {
            return "redirect:/";
        }
        
        Page<OpsTask> topTasks = opsTaskService.findTopTasks(
            uuid,
            report.getHospitalId(),
            report.getDemandYear(),
            page,
            3
        );
        
        model.addAttribute("report", report);
        model.addAttribute("topRequests", topTasks);
        model.addAttribute("reportUuid", uuid);
        model.addAttribute("currentPage", page);
        return "top-requests";
    }
    
    @GetMapping("/reports/{uuid}/latest-ops")
    public String viewLatestOps(@PathVariable String uuid, Model model) {
        Report report = reportService.getReportByUuid(uuid);
        OpsRequest latestRequest = reportService.getLatestRequest(report.getId());
        model.addAttribute("report", report);
        model.addAttribute("latestRequest", latestRequest);
        return "latest-ops";
    }
    
    @GetMapping("/reports/{uuid}/inspection-stats")
    public String viewInspectionStats(@PathVariable String uuid, Model model) {
        Report report = reportService.getReportByUuid(uuid);
        model.addAttribute("report", report);
        return "inspection-stats";
    }
    
    @GetMapping("/reports/{uuid}/thanks")
    public String viewThanks(@PathVariable String uuid, Model model) {
        Report report = reportService.getReportByUuid(uuid);
        model.addAttribute("report", report);
        return "thanks";
    }

    @GetMapping("/yearEndReview")
    public String yearEndReview(Model model) {
        return "generate-yearEndReview";
    }

} 